<?php

class LoaiHoaHongController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }
    
        if ($action == 'remove') {
            // mảng danh sách giá trị id
            $values = $_POST["id"];
 
            $result = $this->deleteLoaiHoaHong($values);
           
            
            if ($result == OK) {
//                Utility::messageBox('Xóa thành công');
                 echo'OK';
            
            }
             exit();
        } else if ($action == 'add') {
            $LOAI_HH=$_POST['LOAI_HH'];
            $LHH_TIEN_MAT = $_POST['LHH_TIEN_MAT'];
            $LHH_PHAN_TRAM = $_POST['LHH_PHAN_TRAM'];
            $result = $this->addLoaiHoaHong($LOAI_HH,$LHH_TIEN_MAT,$LHH_PHAN_TRAM);
            if ($result == OK) {
                if ($action == 'add') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_dm_loaihoahong");
                }
            }
             exit();
             
             
        } else if ($action == 'edit' ) {

            $LHH_ID=$_POST['LHH_ID'];
            $LHH_TEN_LOAI=$_POST['LHH_TEN_LOAI'];
            $LHH_TIEN_MAT = $_POST['LHH_TIEN_MAT'];
            $LHH_PHAN_TRAM = $_POST['LHH_PHAN_TRAM'];
            $result = $this->updateLoaiHoaHong($LHH_ID,$LHH_TEN_LOAI,$LHH_TIEN_MAT,$LHH_PHAN_TRAM);
            if ($result == OK) {
                if ($action == 'edit') {
                    echo 'OK';
                } else {
                    Utility::redirect("./?com=com_dm_loaihanghoa");
                }
            }
             exit();
        }


        if ($task == 'add') {
            $xtemplate = new XTemplate('./components' . DS . 'com_dm_loaihoahong' . DS . 'views' . DS . 'loaihoahong_add.php');
 
            
            
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
        // nếu task là edit thì include trang thêm vào
        else if ($task == 'edit') {
            $id = $_GET['idloaihoahong'];
            $viewFile = $dir . '/views/loaihoahong_edit.php';
            $xtemplate = new XTemplate($viewFile);
            $hanghoa = $this->getLoaiHoaHongById($id);
//            Utility::messageBox($id);

            if ($hanghoa != null) {
                

                    $xtemplate->assign('LHH_ID', $hanghoa->getLHH_ID());
                    $xtemplate->assign('LHH_TEN_LOAI', $hanghoa->getLHH_TEN_LOAI());
                     $xtemplate->assign('LHH_PHAN_TRAM',$hanghoa->getLHH_PHAN_TRAM());                   
                     $xtemplate->assign('LHH_TIEN_MAT', $hanghoa->getLHH_TIEN_MAT());           
                               
                    
                  
                    $xtemplate->parse('main.loaihoahong');
                   
                
            }




            $xtemplate->parse('main');
            $xtemplate->out('main');
            
            
        } 
        else {
            $viewFile = $dir . '/views/loaihoahong.php';
            $xtemplate = new XTemplate($viewFile);
            $loaiHoaHongs = $this->getAllLoaiHoaHong();


            if ($loaiHoaHongs != null) {
                  
                foreach ($loaiHoaHongs as $loaiHoaHong) {

                    $xtemplate->assign('LHH_ID', $loaiHoaHong->getLHH_ID());
                    $xtemplate->assign('LHH_TEN_LOAI', $loaiHoaHong->getLHH_TEN_LOAI()); 
                    if($loaiHoaHong->getLHH_PHAN_TRAM()!=''){
                         $xtemplate->assign('LHH_PHAN_TRAM', 'Phần trăm(%): '.$loaiHoaHong->getLHH_PHAN_TRAM()); 
                    }else{
                        $xtemplate->assign('LHH_PHAN_TRAM', ''); 
                    }
                    if($loaiHoaHong->getLHH_TIEN_MAT()!=''){
                        $xtemplate->assign('LHH_TIEN_MAT', 'Tiền mặt(VNĐ): '.  Utility::formatToCurrencyString($loaiHoaHong->getLHH_TIEN_MAT()));
                    }else{
                         $xtemplate->assign('LHH_TIEN_MAT', '');
                    
                    }
                                     
                     
                    $xtemplate->parse('main.loaihoahong');
                    
                }
            }
         
            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

    private function getAllLoaiHoaHong() {
        $db = new Database();
        try {
            $loaihoahong = new LoaiHoaHong();

            $result = $loaihoahong->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
  private function getLoaiHoaHongById($id) {
        $db = new Database();
        try {
            $loaiHoaHong = new LoaiHoaHong();
            $loaiHoaHong->setLHH_ID($id);
            $result = $loaiHoaHong->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    private function addLoaiHoaHong($LOAI_HH,$LHH_TIEN_MAT,$LHH_PHAN_TRAM){
        
         $db = new Database();
        try {            
           
                $loaiHoaHong = new LoaiHoaHong();
                $loaiHoaHong->setLHH_TEN_LOAI($LOAI_HH);
                $loaiHoaHong->setLHH_PHAN_TRAM($LHH_PHAN_TRAM);
                $loaiHoaHong->setLHH_TIEN_MAT($LHH_TIEN_MAT);
                 $loaiHoaHong->setIS_ACTIVE(1);
                $loaiHoaHong->insert($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
        private function updateLoaiHoaHong($LHH_ID,$LHH_TEN_LOAI,$LHH_TIEN_MAT,$LHH_PHAN_TRAM){
        
         $db = new Database();
        try {            
           
                $loaiHoaHong = new LoaiHoaHong();
                 $loaiHoaHong->setLHH_ID($LHH_ID);
                $loaiHoaHong->setLHH_TEN_LOAI($LHH_TEN_LOAI);
             $loaiHoaHong->setLHH_PHAN_TRAM($LHH_PHAN_TRAM);
                $loaiHoaHong->setLHH_TIEN_MAT($LHH_TIEN_MAT);
                 $loaiHoaHong->setIS_ACTIVE(1);
                $loaiHoaHong->update($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
        
    }
    
    
        private function deleteLoaiHoaHong($values) {
        $db = new Database();
        try {            
           
                $hanghoa = new LoaiHoaHong();                
                $hanghoa->setLHH_ID($values);
                $hanghoa->delete($db);
            
            $db->submitChanges();
            return OK;
        }
        catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    
    
    
    
    
    
}
